/**
 * Copyright (C) 2015 by Liangliang Nan (liangliang.nan@gmail.com)
 * https://3d.bk.tudelft.nl/liangliang/
 *
 * This file is part of Easy3D. If it is useful in your research/work,
 * I would be grateful if you show your appreciation by citing it:
 * ------------------------------------------------------------------
 *      Liangliang Nan.
 *      Easy3D: a lightweight, easy-to-use, and efficient C++
 *      library for processing and rendering 3D data. 2018.
 * ------------------------------------------------------------------
 * Easy3D is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 3
 * as published by the Free Software Foundation.
 *
 * Easy3D is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef EASY3D_ALGO_SURFACE_MESH_PLANAR_PARTITION_H
#define EASY3D_ALGO_SURFACE_MESH_PLANAR_PARTITION_H

#include <easy3d/core/types.h>
#include <easy3d/core/surface_mesh.h>

#include <string>


namespace easy3d {

    class SurfaceMeshPlanarPartition {
    public:
        SurfaceMeshPlanarPartition(SurfaceMesh *mesh);
        ~SurfaceMeshPlanarPartition(void);

        /**
         * Result will save as an <int> type face property.
         * @param partition_name The name of the result face property.
         */
        void apply(const std::string& partition_name);

    private:

        // max_allowed_squared_deviation: for all the 1-ring neighbors of v
        void propagate(SurfaceMesh::Vertex v, int id, float max_allowed_squared_deviation);

        // can grow if one of its incident faces has not been grouped into a planar segment.
        bool can_grow(SurfaceMesh::Vertex v) const;

        // for all the vertices in a face
        float max_squared_dist_to_plane(SurfaceMesh::Face f, const Plane3& plane) const;

    private:
        SurfaceMesh *mesh_;

        SurfaceMesh::FaceProperty<int> planar_segments_;
        SurfaceMesh::VertexProperty<vec3> vertex_normal_;
    };


} // namespace easy3d

#endif  // EASY3D_ALGO_SURFACE_MESH_PLANAR_PARTITION_H